package com.feinno.urcs.group.interop.service;

/**
 * @author Li.Hongbo <lihongbo@feinno.com>
 */

import com.feinno.database.DataRow;
import com.feinno.database.DataTable;
import com.feinno.database.Database;
import com.feinno.urcs.group.interop.FxDataValidateGGrpDao;
import com.feinno.urcs.group.interop.entity.DataValidateGroupBaseInfo;
import org.helium.framework.annotations.FieldSetter;
import org.helium.framework.annotations.ServiceImplementation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;


/**
 * @author Li.Hongbo <lihongbo@feinno.com>
 */
@ServiceImplementation
public class FxDataValidateGGrpDaoImpl implements FxDataValidateGGrpDao {
	private static final Logger LOGGER = LoggerFactory.getLogger(FxDataValidateGGrpDaoImpl.class);

	@FieldSetter("${GGRPDB}")
	private Database ggrpDb;

	@Override
	public DataValidateGroupBaseInfo getGroupConversationIdByGroupId(int groupId) throws Exception {
		DataValidateGroupBaseInfo conversationId = null;
		String sql = "SELECT  GroupId,LogicPool,ConversationId,GroupType FROM GGRP_SearchGroup where GroupId=? ";
		try {
			DataTable dt = ggrpDb.executeTable(sql, groupId);
			if (dt != null && dt.getRows().size() > 0) {
				DataRow dr = dt.getRow(0);
				int logicalPool = dr.getInt("LogicPool");
				String cid = dr.getString("ConversationId");
				int groupType = dr.getInt("GroupType");
				try {
					conversationId = new DataValidateGroupBaseInfo(groupId, logicalPool, cid, groupType);
				} catch (Exception e) {
					LOGGER.error(String.format("groupId=%s  LogicalPool=%s ConversationId=%s groupType=%s", groupId, logicalPool, cid, groupType), e);
				}
			}
		} catch (Exception e) {
			LOGGER.error(String.format("GGRPDB getConversationId error groupId=%s", groupId), e);
		}

		return conversationId;

	}


	public int upConversationId(int groupId, String conversationId) throws SQLException {
		String sql = "update GGRP_SearchGroup set ConversationId=?  where GroupId =?";
		int ret = -1;
		try {
			ret = ggrpDb.executeNonQuery(sql, conversationId, groupId);

		} catch (Exception e) {
			LOGGER.error(String.format("GGRPDB getConversationId error groupId=%s", groupId), e);
		}

		return ret;

	}

}
